var user = liwe.module ( "user", { "change_password": "_change_password" } );

user._uid = -1;
user.events = {
	"login"  : null,
	"logout" : null,
	"registered" : function () { alert ( "Utente registrato con successo" ); }
};

user._evt_login = function ()
{
	var res = false;

	if ( user.events [ 'login' ] ) res = user.events [ 'login' ] ();

	return false;
};

user.init = function ( cback )
{
	user.valid_events ( [ 'login', 'logout', 'register_check', 'register', 'lostpwd', 'rendered', 'profile', 'modify', 'stat-login', 'stat-logout' ] ); 
	user.load_templates ( cback );
	user.addEventListener ( 'lostpwd', user._lostpwd_defalert, true );
	user.addEventListener ( 'stat-login', function ( e ) 
	{
		var p = e.params, txt = "UID: %(uid)s - Extra: %(extra)s";
		liwe.ga.event ( 'User', 'login', String.formatDict ( txt, p ) );
	}, true ); 
	user.addEventListener ( 'stat-logout', function ( e )  
	{
		liwe.ga.event ( 'User', 'logout', e.params.uid );
	}, true ); 
};


//{{{NEW FUNCTION LOGIN FORM
//{{{user.render_login =  function ( dest_div, lostpwd, module )
user.render_login =  function ( dest_div, lostpwd, module )
{
	if ( typeof ( lostpwd ) == 'undefined' ) lostpwd = false;
	if ( typeof ( module ) == 'undefined' ) module = 'user';

	var get =  { 'action' : 'user.ajax.get_info' };
	user.ajax ( get , function ( res )
	{
		res [ 'module' ] =  module;
		if ( res.get ( 'uid' , -1 ) != -1 )
		{
			user.dispatchEvent ( "stat-login", null, res );
			user.dispatchEvent ( "login", null, res );
		}else{
			$( dest_div, user.templates [ 'SITE_LOGIN_BOX' ] );
			user._make_login_form( lostpwd, module );
			user._make_registration_form  ( module );
			user.dispatchEvent ( "rendered", null, res );
		}
	} );
}
//}}}
//{{{user._make_login_form = function ( lostpwd, module )
user._make_login_form = function ( lostpwd, module )
{
	frm = new liwe.form.instance ( "user-login-form" );
	frm.hidden ( { name: "module", value: module } );
	frm.text ( { label: "Login / Email", name: "login", size: 7, maxlength: 50, mandatory: true, onkeydown: "user.key_down(event, this)" } );
	frm.password ( { label: "Password", name: "pwd", size: 7, maxlength: 30, mandatory: true, onkeydown: "user.key_down(event, this)" } );
	frm.button ( { value: "Accedi", onclick: "user.login()"  } );
	if ( ! lostpwd  )
	{
		frm.set ( "user_login" );
		return;
	};
	frm.descr ( "Hai dimenticato la password?" );
	frm.text ( { label: "Tua e-mail", name: "email", size: 7, maxlength: 50 } );
	frm.button ( { value: "Richiedi", onclick: "user.lost_password()", 'class': 'blue' } );
	frm.set ( "user_login" );
	return;
}
//}}}
//{{{user._make_registration_form = function ( module )
user._make_registration_form = function ( module )
{
	if ( typeof ( $( 'user_new' ) ) == 'undefined' ) return; 
	var frm =  liwe.form.get ( "user-login-register" );
	if ( typeof ( frm ) != 'undefined' )
	{
		frm.set ( "user_new" );
	};

	frm = new liwe.form.instance ( "user-login-register" );
	
	frm.hidden ( { name: "module", value: module } );
	frm.email ( { label: "Indirizzo e-mail", name: "email", size: 7, maxlength: 200, mandatory: true } );
	frm.password ( { label: "Password", name: "pwd", size: 7, maxlength: 30, mandatory: true } );
	frm.password ( { label: "Password (conferma)", name: "pwd2", size: 7, maxlength: 30, mandatory: true } );
	frm.button ( { value: "Registrati", onclick: "user.register()" } );
	frm.set ( "user_new" );
	return;	
}
//}}}
//{{{user.key_down = function ( evt, el )
user.key_down = function ( evt, el )
{
	evt = evt || window.event;

	if ( evt.which == 13 || evt.keyCode == 13 )
	{
		if ( el.name == "login" )
		{
			var f  = liwe.form.get ( "user-login-form" );
			var pwd = f.get_element ( 'pwd' );
			pwd.focus();
			return
		}
		user.login ();
	}
};
//}}}
//{{{user.login = function ()
user.login = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	if ( ! frm.check () ) return;
	var a = f.get_values ();
	var cback;

	a [ 'action' ] = "user.ajax.login";
	user.ajax ( a, function ( v ) {
		v [ 'module' ] = a [ 'module' ];
		user._uid = v [ 'uid' ];
		user.dispatchEvent ( "stat-login", null, v );
		user.dispatchEvent ( "login", null, v );
	} );
};
//}}}
//{{{user.register = function ()
user.register = function ()
{
	var frm = liwe.form.get ( "user-login-register" );
	if ( ! frm.check () ) return;
	var vals = frm.get_values ();

	if ( vals [ 'pwd' ] != vals [ 'pwd2' ] )
	{
		var dct = { 'alert' : user.templates [ 'SITE_REG_MSG_PWD' ], 'module' : vals [ 'module' ] } 
		$( 'alert-registration' , dct [ 'alert' ] );	
		user.dispatchEvent ( "register_check", null, dct );
		return;
	}

	user.ajax ( { action: "user.ajax.login_exists", email: vals [ 'email' ] }, function ( v )
	{
		v [ 'module' ] = vals [ 'module' ];
		if ( v [ 'exists' ] )
		{
			v [ 'alert' ] = user.templates [ 'SITE_REG_MSG_MAIL' ]
			$( 'alert-registration' , v [ 'alert' ] );	
			user.dispatchEvent ( "register_check", null, v );
			return;
		}

		if ( ! vals [ 'login' ] ) vals [ 'login' ] = vals [ 'email' ];

		vals [ 'action' ] = 'user.ajax.register';
		user.ajax ( vals, function ( v ) 
		{
			v [ 'email' ] = vals [ 'email' ];
			v [ 'module' ] = vals [ 'module' ];
			user.dispatchEvent ( "register", null, v );
		} );
	} );
};
//}}}
//{{{user.lost_password = function ()
user.lost_password = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var email = f.get_value ( "email" );

	if ( ! email ) return;

	user.ajax ( { action: "user.ajax.lost_password", email: email }, function ( v )
	{
		var res = { 'alert' : String.formatDict ( user.templates [ 'SITE_LOST_PWD' ] , { 'email' : email } ) }; 
		user.dispatchEvent ( "lostpwd", null, res );
	}, function ( txt, err_text, err_code, res )
	{
		var res = { 'alert' : err_text }; 
		user.dispatchEvent ( "lostpwd", null, res );
	} );
};
//}}}
user._lostpwd_defalert = function ( evt )
{
	var p = evt.params;
	alert ( p [ 'alert' ]  );
}
//{{{user.render_profile = function ( dest_div, dct )
user.render_profile = function ( dest_div, dct )
{
	var render = function ( data )
	{
		user.__info	 = data;
		data [ '_DEFAULT' ] =  String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], data );
		$( dest_div , String.formatDict ( user.templates [ 'SITE_PROFILE_BOX' ], data ) );	
		user.dispatchEvent ( "profile", null, data );
	};

	if ( typeof ( dct ) == 'undefined' )
	{
		var post =  { 'action' : 'user.ajax.get_info' };
		user.ajax ( post, function ( res )
		{
			if ( res.get ( 'uid' , -1 ) != -1 )
				return render ( res );
		} );
	} else {
		return render ( dct );
	}
};
//}}}
//{{{user.modify_profile_form = function ()
user.modify_profile_form = function ()
{
	frm = new liwe.form.instance ( "user-profile-form" );
	frm.text ( { label: "Login", name: "login", size: 7, maxlength: 50, mandatory: true } );
	frm.email ( { label: "email", name: 'email', mandatory : true  } );
	frm.password ( { label: "Vecchia password", name: "oldpwd", size: 7, maxlength: 30 , mandatory : true } );
	frm.password ( { label: "Password", name: "pwd", size: 7, maxlength: 30 } );
	frm.password ( { label: "Conferma Password", name: "pwd2", size: 7, maxlength: 30 } );
	frm.button ( { value: "Aggiorna", onclick: "user.modify()", nonl : true } );
	frm.button ( { value: "Annulla", onclick: "user.modify( true )" } );
	$( 'profile_info' , user.templates [ 'SITE_PROFILE_BOX_MODIFY' ] );
	frm.set ( 'modify_form' );
};
//}}}
//{{{user.modify = function( exit )
user.modify = function( exit )
{

	if ( exit )
	{
		$( 'profile' , String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], user.__info ) );
		return;	
	};

	var frm = liwe.form.get ( "user-profile-form" );
	if ( ! frm.check () ) return;

	var post = frm.get_values ();


	if ( post [ 'pwd' ] != post [ 'pwd2' ] )
	{
		$ ( 'err_descr', 'Le nuove password non coincidono' )
		return;
	}


	post [ 'action' ] = 'user.ajax.modify';
	user.ajax ( post , function ( resp )
	{
		$( 'profile' , String.formatDict ( user.templates [ 'SITE_PROFILE_BOX_CNT' ], resp ) );	

		user.dispatchEvent ( "modify", null, resp );

	},function ( txt, err_descr, err_code, data )
	{
		$ ( 'err_descr', err_descr )
		return;	
	} );

}
//}}}
//{{{user.profile_add_box = function ( html, module_name )
user.profile_add_box = function ( html, module_name )
{
	var ws = $( 'profile_ws' );
	el = document.createElement ( "div" );
	el.id = module_name + '_profile';
	el.className = "profile_box"
	el.innerHTML = html;
	ws.appendChild ( el );
};
//}}}
//}}}
//{{{OLD FUNCTION REMIXED
user.logout = function ()
{
	var get =  { 'action' : 'user.ajax.get_info' };
	user.ajax ( get , function ( res )
	{
		
	} );
	user.ajax ( { action: "user.ajax.logout" }, function () 
	{
		//retrocompatibility
		if ( user.events.logout ) user.events.logout ();

		user.dispatchEvent ( "stat-logout", null, { 'uid' : user._uid } );
		user._uid = -1; 
		//new event dispatch
		user.dispatchEvent ( "logout", null, { 'res' : 'ok' } );

	} );
};
//}}}
//{{{OLD FUNCTION FOR COMPATIBILITY
user.login_box = function ()
{
	var frm = user.login_form();
	//liwe.lightbox.easy ( "user-lightbox", "Accesso utente", 450, 300, -1, 100 );
	liwe.lightbox.easy ( "user-lightbox", "Accesso utente", 343, 350 );

	frm.set ( "user-lightbox" );

};

user.login_form = function ( cback )
{
	var frm =  liwe.form.get ( "user-login-form" );

	if ( frm ) 
	{
		frm._login_cback = cback;
		return frm;
	}

	frm = new liwe.form.instance ( "user-login-form" );

	frm._login_cback = cback;

	frm.text ( { label: "Login / Email", name: "login", size: 20, maxlength: 50, mandatory: true, onkeydown: "user._key_down(event)" } );
	frm.password ( { label: "Password", name: "pwd", size: 20, maxlength: 30, mandatory: true, onkeydown: "user._key_down(event)" } );
	frm.button ( { value: "Accedi", onclick: "user._login()", 'class': 'green' } );
	frm.sep ();
	frm.descr ( "Hai dimenticato la password?" );
	frm.text ( { label: "Tua e-mail", name: "email", size: 20, maxlength: 50 } );
	frm.button ( { value: "Richiedi", onclick: "user._lost_password()", 'class': 'blue' } );
	frm.sep ();
	frm.descr ( "Registrazione nuovo utente" );
	frm.label ( { value: "&nbsp;<br />La registrazione a questo sito &egrave; totalmente gratuita!<br />&nbsp;<br />" } );
	frm.button ( { value: "Vuoi registrarti al sito? Clicca qui!", onclick: "user.registration()", 'class': 'yellow' } );
	if ( typeof ( social ) != 'undefined' )
	{
		social.facebook.cbacks [ 'login' ] = user._facebook_login;
		social.facebook.cbacks [ 'init' ] = user._facebook_init;
		frm.label ( { value: social.facebook.get_login () } );
	}
	return frm;
};

user.registration_form = function ( clear )
{
	var frm =  liwe.form.get ( "user-login-register" );
	if ( typeof ( frm ) != 'undefined' )
	{
		if ( clear ) frm.clear ();
		return frm;
	};

	frm = new liwe.form.instance ( "user-login-register" );
	
	frm.email ( { label: "Indirizzo e-mail", name: "email", size: 30, maxlength: 200, mandatory: true } );
	frm.password ( { label: "Password", name: "pwd", size: 30, maxlength: 30, mandatory: true } );
	frm.password ( { label: "Password (conferma)", name: "pwd2", size: 30, maxlength: 30, mandatory: true } );
	frm.button ( { value: "Registrati", onclick: "user._register()" } );
	
	return frm;
	
};

user.registration = function ()
{
	var frm = user.registration_form();

	frm.set ( "user-lightbox" );

};

user._register = function ()
{
	var frm = liwe.form.get ( "user-login-register" );
	var vals = frm.get_values ();

	if ( vals [ 'pwd' ] != vals [ 'pwd2' ] )
	{
		alert ( "Verifcare la password inserita" );
		return;
	}

	user.ajax ( { action: "user.ajax.login_exists", email: vals [ 'email' ] }, function ( v )
	{
		if ( v [ 'exists' ] )
		{
			alert ( "L'indirizzo di email inserito e' gia' presente nel nostro database." );
			return;
		}

		if ( ! vals [ 'login' ] ) vals [ 'login' ] = vals [ 'email' ];

		vals [ 'action' ] = 'user.ajax.register';
		user.ajax ( vals, function ( v ) 
		{
			v [ 'email' ] = vals [ 'email' ];
			user.events [ 'registered' ] && user.events [ 'registered' ] ( v );
			liwe.lightbox.close ();
		} );
	} );
};

user._change_password = function ( data )
{
	if ( ! data || typeof ( data ) != 'object' || ! data [ 'token' ] ) return;
	user.change_password ( data [ 'email' ], data [ 'token' ] );
};

user.change_password = function ( email, token )
{
	var frm = new liwe.form.instance ( "user-change-pwd-form" );
	frm.password ( { label: "Password", name: "pwd", size: 12, maxlength: 30, mandatory: true } );
	frm.password ( { label: "Password (verifica)", name: "pwd2", size: 12, maxlength: 30, mandatory: true } );
	frm.descr ( '' );
	frm.descr ( 'Inserisci qui la nuova password per il tuo account.' );
	frm.descr ( 'Riceverai una mail con i nuovi dati per accedere al sito' );
	frm.descr ( '' );
	frm.hidden ( { name: "email", value: email } );
	frm.hidden ( { name: "token", value: token } );
	frm.button ( { value: "Salva", onclick: "user._change_pwd()" } );

	liwe.lightbox.easy ( "user-lightbox", "Modifica password", 300, 80 );

	frm.set ( "user-lightbox" );
};

user._change_pwd = function ()
{
	var f = liwe.form.get ( "user-change-pwd-form" );
	var a;

	if ( ! f.check () ) return;

	a = f.get_values ();

	if ( a [ 'pwd' ] != a [ 'pwd2' ] ) 
	{
		alert ( "Le due password inserite non coincidono" );
		return;
	}

	user.ajax ( { action: "user.ajax.change_password", pwd: a [ 'pwd' ], token: a [ 'token' ], email : a [ 'email' ] }, function ( v )
		{
			alert ( "Password aggiornata!" );
			liwe.lightbox.close ();
		} );

};


user._login = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var a = f.get_values ();
	var cback;

	a [ 'action' ] = "user.ajax.login";

	user.ajax ( a, function ( v ) {
		if  ( liwe.lightbox.created() )	liwe.lightbox.close ();
		user._uid = v [ 'uid' ];

		cback = f._login_cback;
		f._login_cback = null;

		if ( cback )
		{
			cback ( v );
		} else {
			if ( user.events.login ) user.events.login ( v );
		}
	} );
};

user._lost_password = function ()
{
	var f = liwe.form.get ( "user-login-form" );
	var email = f.get_value ( "email" );

	if ( ! email ) return;

	user.ajax ( { action: "user.ajax.lost_password", email: email }, function ( v )
		{
			alert ( "Una nuova password e' stata inviata all'indirizzo di email: " + email );
		} );
};

user._key_down = function ( e )
{
	e = e || window.event;

	if ( e.which == 13 || e.keyCode == 13 )
		user._login ();
};

user._facebook_login = function ( res )
{
	social.facebook.user_login ( function ( res )
	{
		if ( res.text != 'OK' )
		{
			alert ( res.text );
		}
		window.location.reload ();
	} );
};

user._facebook_init = function ()
{
	if ( social.facebook.is_logged () )
	{
		var data = social.facebook.get_user ();
		$ ( 'social-facebook-text', 'Login come ' + data.name );
	}
};
//}}}
